package com.example.controller;

import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpClientExample {
    public static void main(String[] args) {
        try {
            // 创建URL对象，指定需要发送请求的网址
            URL url = new URL("http://www.gudianmingzhu.com/zuozhe/16911.html");

            // 打开连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            // 设置请求方法为GET
            connection.setRequestMethod("GET");

            // 设置请求头信息
            connection.setRequestProperty("User-Agent", "Mozilla/5.0");

            // 获取响应码
            int responseCode = connection.getResponseCode();

            // 如果响应码为200，表示请求成功
            if (responseCode == HttpURLConnection.HTTP_OK) {
                // 获取响应流
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line;
                StringBuilder response = new StringBuilder();

                // 读取响应内容
                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }
                reader.close();

                // 打印响应内容
                System.out.println(response.toString());

            }
            else {
                System.out.println("请求失败，响应码：" + responseCode);
            }
            // 关闭连接
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
